﻿using System;
using System.Web.Script.Serialization;

namespace ARS.PublicTool.DataHandle
{
    public class JsonDeSerializer
    {
        private static JavaScriptSerializer ser = new JavaScriptSerializer();
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="json"></param>
        /// <param name="targetType"></param>
        /// <returns></returns>
        public static object Deserialize(string json)
        {
            return ser.DeserializeObject(json);
            //下面代码适用于.Net Framework 4.0
            //return ser.Deserialize(json, targetType);
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="json"></param>
        /// <param name="targetType"></param>
        /// <returns></returns>
        public static object Deserialize(string json, Type targetType)
        {
            //return ser.DeserializeObject(json);
            //下面代码适用于.Net Framework 4.0
            return ser.Deserialize(json, targetType);
        }

        /// <summary>
        /// json字符串转为对象实体
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T JsonToObj<T>(string json)
        {
            return ser.Deserialize<T>(json);
        }
    }
}
